

export default {
  isEmpty(v) {
    return v == undefined || v == null;
  },
  appendFilter(filters, property, operator, value) {
    if (value || value == false && value !== '') filters.push({ property, operator, value });
    return filters;
  },
  appendFilterDataTime(filters, property, values) {
    if (values && values.length > 0) filters.push({ property, operator: "ge", value: values[0] });
    if (values && values.length > 1) filters.push({ property, operator: "le", value: values[1] });
    return filters;
  },
  appendFilterMultipleInput(filters, multipleInput) {
    if (multipleInput && multipleInput.name) {
      let property = multipleInput.name;
      let operator = multipleInput.operator ? multipleInput.operator : "like"
      let value = multipleInput.values[multipleInput.name];
      if (value) {
        filters.push({ property, operator, value })
      }
    }
    return filters;
  },
  appendFilterIntevalInput(filters, property, intevalInput) {
    if (intevalInput) {
      if (!this.isEmpty(intevalInput.min)) filters.push({ property, operator: "ge", value: intevalInput.min });
      if (!this.isEmpty(intevalInput.max)) filters.push({ property, operator: "le", value: intevalInput.max });
    }
    return filters;
  },
  appendFilterStatus(filters, property, values) {
    if (!values || values.length == 0) {
      return filters;
    }
    if (values.length == 1) {
      filters.push({ property, operator: "eq", value: values[0] });
    } else {
      filters.push({ property, operator: "in", value: values });
    }
    return filters;
  },

  initPage() {
    return {
      current: 1,
      size: 10,
      total: 0,
      ascs: [],
      descs: [],
      filters: [],
      records: [],
    };
  },
  assemblePage(table) {
    let p = table.page
    let hasOrder = (p.ascs && p.ascs.length > 0) || (p.descs && p.descs.length > 0)
    return {
      body: {
        current: p.current ? p.current : 1,
        size: p.size ? p.size : 10,
        ascs: hasOrder ? p.ascs : table.sort.ascs,
        descs: hasOrder ? p.descs : table.sort.descs,
        filters: [], //清空filter，页面独立处理
      }
    };
  },

  assembleBody(page, body) {
    if (body) {
      page.total = parseInt(body.total ? body.total : "0");
      page.records = body.records ? body.records : [];
    } else {
      page.total = 0;
      page.records = [];
    }
    return page;
  },

  newAddItem(page) {
    if (page && page.records && page.records.find(item => item.is_New_Add)) {
      return page;
    }
    if (page && page.records) {
      page.records.push({
        editing: true,
        is_New_Add: true
      });
    }
    return page;
  },

  getParameter(key) {
    return this.$route.query[key];
  },
  link(name, query) {
    this.$router.push({ name, query });
  },
  inArray(value, values) {
    return values.indexOf(value) >= 0;
  },

  translate(values, adapter) {
    let result = [];
    if (adapter) {
      values.forEach(element => {
        result.push(adapter(element));
      });
    } else {
      result = values;
    }
    return result;
  },

  parseImagePaths(value, isArray) {
    if (!value) return [];
    let paths = [];
    if (isArray) {
      for (const v of value) {
        paths.push({ url: v });
      }
    } else {
      paths.push({ url: value });
    }
    return paths;
  },

  export(res, filename) {
    // let blob = new Blob([res.data], { type: 'application/vnd.ms-excel,charset=UTF-8' });
    // let blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,charset=UTF-8' });
    let blob = new Blob([res.data], { type: 'charset=UTF-8' });

    filename=filename ? filename : new Date().getTime()

    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(blob, filename);
    }else {
        let url = window.URL.createObjectURL(blob);
        let link = document.createElement("a");
        document.body.appendChild(link); 
        link.style.display = "none";
        link.href = url;
        link.setAttribute("download", filename + '.xlsx');
        link.click();
        document.body.removeChild(link);
        window.URL.revokeObjectURL(url);
    }



    // 后端代码
    // response.setContentType("application/vnd.ms-excel");
    // response.setContentType("application/vnd.ms-excel; charset=UTF-8");
    // response.setHeader("Content-Disposition", "attachment; filename=001.xls");
    // response.addHeader("Pargam", "no-cache");
    // response.addHeader("Cache-Control", "no-cache");
    // out = response.getOutputStream();
    // wb.write(out);
  },


}; 